package com.expressage.controller;

import com.expressage.po.*;
import com.expressage.po.Package;
import com.expressage.service.*;
import com.expressage.vo.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 物流控制器
 */
@Controller
public class FlowController {

    Logger logger = Logger.getLogger(FlowController.class);

    @Autowired
    FlowService flowService;
    @Autowired
    PackageService packageService;
    @Autowired
    StateService stateService;
    @Autowired
    SiteService siteService;
    @Autowired
    StaffService staffService;
    @Autowired
    RecipientService recipientService;
    @Autowired
    SendMessService sendMessService;
    @Autowired
    SortService sortService;
    @Autowired
    UserService userService;


    @RequestMapping("/flowlist")
    public String flowList(Model model){
        logger.info("物流列表控制器");
        List<FlowVO> list = flowService.findAll();
        model.addAttribute("flowlist",list);
        return "jspafter/flowlist";
    }
    @RequestMapping("/toaddflow")
    public String toAddFlow(Model model){
        logger.info("去添加物流控制器");
       /* List<PackageVO> list = packageService.findAll();
        model.addAttribute("package",list);*/
        List<UserVO> userVO = userService.findAll();
        model.addAttribute("user",userVO);
        List<Site> list1 = siteService.findAll();
        model.addAttribute("site",list1);
        List<State> list2 = stateService.findAll();
        model.addAttribute("state",list2);
        List<StaffVO> list3 = staffService.findByRid(7);
        model.addAttribute("staff",list3);
        List<RecipientVO> list = recipientService.findAll();
        model.addAttribute("recipient",list);
        List<SendMessVO> list4 = sendMessService.findAll();
        model.addAttribute("sendmess",list4);
        List<Sort> list5 = sortService.findAll();
        model.addAttribute("sort",list5);
        return "jspafter/addflow";
    }
    @RequestMapping("/addflow")
    public String addFlow(Flow flow, Package pack){
        logger.info("添加物流控制器");
        System.out.println("pack"+pack.getFid());
        boolean flag1= packageService.addPackage(pack);
        logger.info(flag1?"添加包裹成功":"添加包裹失败");
        List<Package> list =packageService.find(pack);
        for (Package pack1:list) {
            flow.setPid(pack1.getPid());
        }
        boolean flag = flowService.insert(flow);
        logger.info(flag?"添加物流成功":"添加物流失败");
        return "redirect:flowlist.action";
    }
    @RequestMapping("/toupdateflow")
    public String toUpdateFlow(int flowid,Model model){
        FlowVO flowVO = flowService.findByflowid(flowid);
        model.addAttribute("flowVO",flowVO);
        logger.info(flowid);
        List<Site> list1 = siteService.findAll();
        model.addAttribute("site",list1);
        List<State> list2 = stateService.findAll();

        model.addAttribute("state",list2);
        List<StaffVO> list3 = staffService.findByRid(7);
        model.addAttribute("staff",list3);
        return "jspafter/editflow";
    }
    @RequestMapping("/updateflow")
    public String updateFlow(Flow flow){
        logger.info("修改物流状态");
        logger.info("相当于新增物流");
        boolean flag =  flowService.insert(flow);
        logger.info(flag?"修改成功":"修改失败");
        return "redirect:flowlist.action";
    }

    @RequestMapping("/deleteflow")
    public String deleteFlow(int[] array){
        if (array.length>0){
            boolean flag = flowService.delete(array);
            logger.info(flag?"删除成功":"删除失败");
        }
        return "redirect:flowlist.action";
    }
    @RequestMapping("/activeflow")
    public String activeFlow(int flowid){
        logger.info("进入修改状态控制器");
        Flow flow =new Flow();
        flow.setStateid(13);
        flow.setSiteid(16);
        flow.setFlowid(flowid);
        flowService.update(flow);
        return "redirect:flowlist.action";
    }
    @RequestMapping("/flowbyodd")
    public String flowByOdd(String odd,Model model){
        List<FlowVO> list = flowService.findByodd(odd);
        model.addAttribute("list",list);
        return "jspbefore/flowbyodd";
    }
    @RequestMapping("/tosearch")
    public String toSearch(){
        return "jspbefore/search1";
    }
}
